package com.example.myapplication.module

import android.app.Activity
import android.app.AlertDialog
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.example.myapplication.R
import com.example.myapplication.entity.CloudBean
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.SpUtils
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken

class SettingDialog(var activity: Activity) : View.OnClickListener {
    private val choosePopup = ChoosePopup(activity)
    private var dialog: AlertDialog? = null
    lateinit var etIp: EditText
    lateinit var etPort: EditText
    lateinit var etUserName: EditText
    lateinit var tvConnectMethod: TextView
    lateinit var tv3: TextView
    var cloudMap: MutableList<CloudBean> = mutableListOf()

    fun getDialog(cloudMap: MutableList<CloudBean>) {
        this.cloudMap = cloudMap
        if (dialog == null) {
            dialog = AlertDialog.Builder(activity).create()
        }
        val view = View.inflate(activity, R.layout.setting_dialog, null)
        tvConnectMethod = view.findViewById<TextView>(R.id.tvConnectMethod)
        tv3 = view.findViewById<TextView>(R.id.tv3)
        etIp = view.findViewById<EditText>(R.id.etIp)
        etUserName = view.findViewById<EditText>(R.id.etUserName)
        etPort = view.findViewById<EditText>(R.id.etPort)
        val tvConfirm = view.findViewById<TextView>(R.id.tvConfirm)
        val tvCancel = view.findViewById<TextView>(R.id.tvCancel)

        tvConnectMethod.setOnClickListener(this)
        tvConfirm.setOnClickListener(this)
        tvCancel.setOnClickListener(this)
        dialog?.setCancelable(true)
        dialog?.window?.setBackgroundDrawableResource(R.drawable.shape_dialog_bg)
        dialog?.show()
        for (cloudBean in cloudMap) {
            if (cloudBean.cloudCheck) {
                tvConnectMethod.text = cloudBean.cloudName
                etIp.setText(cloudBean.ip)
                etPort.setText(cloudBean.port)
                etUserName.setText(cloudBean.userName)
                if (cloudBean.isPublic) {
                    etPort.visibility = View.INVISIBLE
                    tv3.setText(R.string.userName)
                    etUserName.visibility = View.VISIBLE
                } else {
                    etPort.visibility = View.VISIBLE
                    tv3.setText(R.string.port)
                    etUserName.visibility = View.INVISIBLE
                }
            }
        }
        val params = ViewGroup.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        dialog?.setContentView(view, params)
        dialog?.window?.setGravity(Gravity.CENTER)
//        dialog?.window?.setWindowAnimations(R.style.middleEnterExit)
        dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        val attributes = dialog?.window?.attributes
        // dialog显示时背景的昏暗程度
        attributes?.dimAmount = 0.5f
        attributes?.width = UiTools.getDeviceWidth(activity) * 3 / 4
        dialog?.window?.attributes = attributes
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tvConnectMethod -> {
                choosePopup.getPopup(tvConnectMethod, cloudMap)
                choosePopup.chooseCloud = object : ChoosePopup.ChooseCloud {
                    override fun onChooseCloud(cloudType: CloudBean, position: Int) {
                        tvConnectMethod.text = cloudType.cloudName
                        etIp.setText(cloudType.ip)
                        etPort.setText(cloudType.port)
                        etUserName.setText(cloudType.userName)
                        for (cloudBean in cloudMap) {
                            cloudBean.cloudCheck = cloudBean.cloudName == cloudType.cloudName
                        }
                        when (position) {
                            0 -> {
                                tv3.setText(R.string.port)
                                etUserName.visibility = View.INVISIBLE
                                etPort.visibility = View.VISIBLE
                            }

                            1 -> {
                                tv3.setText(R.string.userName)
                                etPort.visibility = View.INVISIBLE
                                etUserName.visibility = View.VISIBLE
                            }
                        }
                    }
                }
            }
            R.id.tvConfirm -> {
                val cloudList = choosePopup.getCloudList()
                if (cloudList.isNullOrEmpty()) {
                    cloudList.addAll(cloudMap)
                }

                if (etIp.text.toString().trim().isEmpty()) {
                    UiTools.showToast(R.string.inputIp)
                    return
                }

                if (etPort.visibility == View.VISIBLE) {
                    if (etPort.text.toString().trim().isEmpty()) {
                        UiTools.showToast(R.string.inputPort)
                        return
                    }
                }
                if (etUserName.visibility == View.VISIBLE) {
                    if (etUserName.text.toString().trim().isEmpty()) {
                        UiTools.showToast(R.string.inputUserName)
                        return
                    }
                }

                for (cloudBean in cloudList) {
                    if (cloudBean.cloudName == tvConnectMethod.text.toString()
                            .trim() && etPort.visibility == View.VISIBLE
                    ) {
                        cloudBean.port = etPort.text.toString()
                        cloudBean.ip = etIp.text.toString()
                    } else if (cloudBean.cloudName == tvConnectMethod.text.toString()
                            .trim() && etUserName.visibility == View.VISIBLE
                    ) {
                        cloudBean.ip = etIp.text.toString()
                        cloudBean.userName = etUserName.text.toString()
                    }
                }
                // 点击保存服务信息
                SpUtils.put(
                    "serviceInfo",
                    GsonUtil.toJson(
                        cloudList,
                        object : TypeToken<MutableList<CloudBean>>() {}.type
                    )
                )

                confirmClick?.onConfirmClick(cloudList)
                if (dialog?.isShowing == true) {
                    dialog?.dismiss()
                }
            }
            R.id.tvCancel -> {
                if (dialog?.isShowing == true) {
                    dialog?.dismiss()
                }
            }
        }
    }

    interface ConfirmClick {
        fun onConfirmClick(cloudList: MutableList<CloudBean>)
    }

    var confirmClick: ConfirmClick? = null
}